import { getUserOrderList ,initOrder,cancelOrder,deleteOrder,
  getUserOrderDetails,saveOrderEvaluationApp,getOrderEvaluationByOrderId } from '../services/order'


export default {

  namespace: 'orders',

  state: {
      orderList:{},
      order:{},
      commodityList:[],
      payOrderId:false,
      orderNum:false,
      evaluation:{}
  },



  effects: {
    *deleteOrder({ payload }, { call, put }) {  // 删除订单
      console.log(payload)
      const resp = yield call(deleteOrder,payload.cancelQs)
      const resp2 = yield call(getUserOrderList,payload.selQs)
      yield put(
        { type: 'selOrder' ,
          state: {
            orderList: resp2.data.data
          }
        }
      );
      return resp;
    },
    *cancelOrder({ payload }, { call, put }) {  // 取消订单
      const resp = yield call(cancelOrder,payload.cancelQs)
      const resp2 = yield call(getUserOrderList,payload.selQs)
      yield put(
        { type: 'selOrder' ,
          state: {
            orderList: resp2.data.data
          }
        }
      );
      return resp;
    },
    *selOrderList({ payload }, { call, put }) {  // 查询订单
      const resp = yield call(getUserOrderList,payload)
      yield put(
        { type: 'selOrder' ,
          state: {
            orderList: resp.data.data
          }
        }
      );
      return resp;
    },
    *selOrderById({ payload }, { call, put }) {  // 查询订单
      const resp = yield call(getUserOrderDetails,payload)
      yield put(
        { type: 'selOrderId' ,
          state: {
            order: resp.data.data.orderInfo,
            commodityList: resp.data.data.commodityList
          }
        }
      );
      return resp;
    },
    *cancelSelOrderById({ payload },{ call, put }) { //取消刷新订单
      const resp2 = yield call(cancelOrder,payload.cancelQs)
      const resp = yield call(getUserOrderDetails,payload.selQs)
      yield put(
        { type: 'selOrderId' ,
          state: {
            order: resp.data.data.orderInfo,
            commodityList: resp.data.data.commodityList
          }
        }
      );
      return resp;
    },
    *saveEvaluation({ payload },{ call, put }){ //保存评价
      const resp = yield call(saveOrderEvaluationApp,payload)
    },
    *selEvaluation({ payload },{ call, put }){ //保存评价
      const resp = yield call(getOrderEvaluationByOrderId,payload)
        yield put(
          { type: 'selOrderId' ,
          state: {
            evaluation: resp.data.data
          }
        }
      )
    }
  },

  reducers: {
    selOrder(state, action) {
      let rs = {}
      if(action.state.orderList.page===1){
        rs = { ...state, ...action.state }
      }else{
        let tempList = state.orderList.orderList
        tempList = tempList.concat(action.state.orderList.orderList) 
        action.state.orderList.orderList = tempList
        rs = { ...state, ...action.state}
      }
      return rs;
    },
    selOrderId(state, action){
      return {...state, ...action.state}
    }
  },

};
